﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EverBuy.SocialNetworking.BusinessObjects;
using EverBuy.SocialNetworking.Data.EntityFrameworkRepository.Abstracts;
using EverBuy.SocialNetworking.Data.Repository.Abstract;

namespace EverBuy.SocialNetworking.Data.EntityFrameworkRepository
{
    public class TermRepository : RepositoryBase, ITermRepository
    {
        public Term GetCurrentTerm()
        {
            var term = (from t in DataContext.Terms
                        orderby t.CreateDate descending
                        select t).FirstOrDefault();

            return ModelMapper.Mapper.Map(term);
        }

        public void SaveTerm(Term term)
        {
            if (term.TermID > 0)
            {
                //update
            }
            else
            {
                //add
                DataContext.AddToTerms(ModelMapper.Mapper.Map(term));
            }
            DataContext.SaveChanges();
        }

        public void DeleteTerm(Term term)
        {
            DataContext.DeleteObject(term);
            DataContext.SaveChanges();
        }
    }
}
